import {XhssubjectModel} from "../../model/XhssubjectModel";
import {Op} from "sequelize";
import {writeExcel} from "../../excel/excel";
import {isNil, isNull} from "lodash";

const startDate = "2024-03-11 00:00:00";
const endDate = "2024-03-14 23:59:59";
const cookie ='abRequestId=2d8d2eed-26c6-548e-b634-b99c4cbc65df; a1=18ca9bcd566qfdje1d8g7ez7r5u24qhk76rcbsso850000273239; webId=3e7fc3a3810607718376cb1eeb53444a; gid=yYS0jDfyWYWJyYS0jDSf2KMMKKAifVdyfYkWFlYduWF27S28J4AxTD888JWqJqj8fiDKSDWD; xsecappid=xhs-pc-web; customerClientId=947566195687588; x-user-id-pgy.xiaohongshu.com=5ea04823000000000100bc6d; web_session=040069b069bc0956403ea13fdf374b5e259f6c; websectiga=984412fef754c018e472127b8effd174be8a5d51061c991aadd200c69a2801d6; acw_tc=017909f807cce2e647b2dd26154d44c218eb02482c3ee49077816da824e28ded; feratlin-status=online; feratlin-status.sig=uBZJqsDDK9NbcHCALtzq7uIWcElHVIDWaGpKRyVXpts; customer-sso-sid=65f3e530480000000000000a; solar.beaker.session.id=1710482736530055216396; access-token-pgy.xiaohongshu.com=customer.ares.AT-32420594e9ac4073beb501bfd66d1e6d-454a10a583914689b99d445d79922c55; access-token-pgy.beta.xiaohongshu.com=customer.ares.AT-32420594e9ac4073beb501bfd66d1e6d-454a10a583914689b99d445d79922c55';
const fetchurl = (pageNum:number)=>{
    //
    const data ={"searchType":2,"keyword":"万千宠爱傲力给","searchWord":"万千宠爱傲力给","pageSize":20,"pageNum":pageNum,"notePublishTimeStart":startDate,"notePublishTimeEnd":endDate,"sorts":[{"column":"hot","sort":"desc"}]};
    return  fetch("https://pgy.xiaohongshu.com/api/solar/cooperator/blogger/track", {
        "headers": {
            "accept": "application/json, text/plain, */*",
            "accept-language": "zh-CN,zh;q=0.9",
            "authorization": "",
            "content-type": "application/json;charset=UTF-8",
            "sec-ch-ua": "\"Chromium\";v=\"119\", \"Not?A_Brand\";v=\"24\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "x-b3-traceid": "60a2a404657d6861",
            "x-s": "0gqvOYTlOl1+165iZB1i1gMC0gcL12aBOBOJ0jZ6Ols3",
            "x-t": "1705300648890",
            "cookie": cookie,
            "Referer": "https://pgy.xiaohongshu.com/solar/creative/content",
            "Referrer-Policy": "no-referrer-when-downgrade"
        },
        "body": JSON.stringify(data),
        "method": "POST"
    }).then(res=>res.json());
}
const fetchSearch =(pageNum:number,trackId:string)=>{

    const data = {"searchWord":"万千宠爱傲力给","pageSize":20,"pageNum":pageNum,"notePublishTimeStart":startDate,"notePublishTimeEnd":endDate,"sorts":[{"column":"hot","sort":"desc"}],"trackId":trackId};
    return fetch("https://pgy.xiaohongshu.com/api/solar/content_square/searchNote", {
        "headers": {
            "accept": "application/json, text/plain, */*",
            "accept-language": "zh-CN,zh;q=0.9",
            "authorization": "",
            "content-type": "application/json;charset=UTF-8",
            "sec-ch-ua": "\"Chromium\";v=\"119\", \"Not?A_Brand\";v=\"24\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "x-b3-traceid": "5251b96bcc92b0e8",
            "x-s": "sY1lOB5l1B4BZgqUO25lsjUU0jaBs2sisg4vOgk61g93",
            "x-t": "1705300649085",
            "cookie": cookie,
            "Referer": "https://pgy.xiaohongshu.com/solar/creative/content",
            "Referrer-Policy": "no-referrer-when-downgrade"
        },
        "body": JSON.stringify(data),
        "method": "POST"
    }).then(res=>res.json());
}
export const detail =(nodeid:string)=>{
    return  fetch("https://pgy.xiaohongshu.com/api/solar/note/"+nodeid+"/detail?bizCode=content_square_inspire", {
        "headers": {
            "accept": "application/json, text/plain, */*",
            "accept-language": "zh-CN,zh;q=0.9",
            "authorization": "",
            "sec-ch-ua": "\"Chromium\";v=\"119\", \"Not?A_Brand\";v=\"24\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "x-b3-traceid": "43c439c4366f0a09",
            "x-s": "Og4B0j9ps6MKOi5WO2OUZjdB1l1CZ61lsBZ61lOBs6T3",
            "x-t": "1705641368678",
            "cookie": cookie,
            "Referer": "https://pgy.xiaohongshu.com/solar/creative/content",
            "Referrer-Policy": "no-referrer-when-downgrade"
        },
        "body": null,
        "method": "GET"
    }).then(res=>res.json());
}
export const bloggerDetail =(userid:string)=>{
    return  fetch("https://pgy.xiaohongshu.com/api/solar/cooperator/user/blogger/"+userid, {
        "headers": {
            "accept": "application/json, text/plain, */*",
            "accept-language": "zh-CN,zh;q=0.9",
            "authorization": "",
            "sec-ch-ua": "\"Chromium\";v=\"119\", \"Not?A_Brand\";v=\"24\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "x-b3-traceid": "43c439c4366f0a09",
            "x-s": "Og4B0j9ps6MKOi5WO2OUZjdB1l1CZ61lsBZ61lOBs6T3",
            "x-t": "1705641368678",
            "cookie": cookie,
            "Referer": "https://pgy.xiaohongshu.com/solar/creative/content",
            "Referrer-Policy": "no-referrer-when-downgrade"
        },
        "body": null,
        "method": "GET"
    }).then(res=>res.json());
}
/**
 * 抓取列表数据
 * @param pagenum
 */
export const fetchData =async (pagenum:number)=>{
    const res = await fetchurl(pagenum);

    if (res['code']==0){
        const {data} = res;
        const rows = await fetchSearch(pagenum, data['trackId']);
        if (rows['success']){
            const {noteList} = rows['data'];
            if (noteList.length==0){
                console.log("数据抓取完毕");
            }
            if (noteList.length > 0){
                for (const noteListElement of noteList) {
                    const row = await XhssubjectModel.findOne({where: {noteId: {[Op.eq]: noteListElement['noteInfo']['noteId']}}});
                    if (!row){
                        await XhssubjectModel.create({
                            noteId: noteListElement['noteInfo']['noteId'],
                            noteInfo: noteListElement['noteInfo'],
                            userInfo: noteListElement['userInfo'],
                        });
                    }else{
                        await XhssubjectModel.update({
                            noteId: noteListElement['noteInfo']['noteId'],
                            noteInfo: noteListElement['noteInfo'],
                            userInfo: noteListElement['userInfo'],
                        },{where:{id: row.id}});
                    }
                    console.log(noteListElement['noteInfo']['noteId'])
                }
                return true;
            }
        }
    }
    return  false;

}
/**
 * 笔记内容详情
 */
export const fetchNoteContent = async()=>{
    const rows = await XhssubjectModel.findAll({
        where:{
            noteId:{
                [Op.not]:null
            },
            isFetchContent:{
                [Op.eq]:0
            }
        },
        order:[['id','ASC']],
        limit:5
    });
    if (rows.length==0){
        console.log("暂无数据抓取");
        return false;
    }
    for (const row of rows) {
        const note = await detail(row.noteId);
        //console.log(note)
        if (note['code']==0){
            if (note['data']){
                await XhssubjectModel.update({
                    noteContent: note['data'],
                    isFetchContent: 1
                },{where:{id: row.id}});
            }else{
                await XhssubjectModel.update({
                    isFetchContent: 50
                },{where:{id: row.id}});
                console.log("1抓取失败的笔记id ：",row.noteId)
            }
        }else{
            await XhssubjectModel.update({
                isFetchContent: 50
            },{where:{id: row.id}});
            console.log("2抓取失败的笔记id ：",row.noteId,note)
        }
    }
    return true;
    //fetchNoteContent();
}
export const fetchBloggerDetail = async()=>{
    const rows = await XhssubjectModel.findAll({
        where:{
            noteId:{
                [Op.not]:null
            },
            isFetchContent:{
                [Op.eq]:1
            }
        },
        order:[['id','ASC']],
        limit:5
    });
    if (rows.length==0){
        console.log("暂无数据抓取");
        return false;
    }
    for (const row of rows) {
        const note = await bloggerDetail(row.userInfo['userId']);
        //console.log(note)
        if (note['code']==0){
            if (note['data']){
                await XhssubjectModel.update({
                    bloggerDetail: note['data'],
                    isFetchContent: 2
                },{where:{id: row.id}});
            }else{
                await XhssubjectModel.update({
                    isFetchContent: 50
                },{where:{id: row.id}});
                console.log("1抓取小红书博主详情 ：",row.userInfo['userId'])
            }
        }else{
            await XhssubjectModel.update({
                isFetchContent: 50
            },{where:{id: row.id}});
            console.log("2抓取小红书博主详情 ：",row.userInfo['userId'])
        }
    }
    return true;
    //fetchNoteContent();
}
/**
 * 导出数据,生成表格
 */
export const exportSubjectQueryfans =async ()=>{
    //{where: {noteId: {[Op.eq]: '65855bd70000000009024083'}}}
    const rows = await XhssubjectModel.findAll();
    const list =[];
    for (const row of rows) {
        const bloggerDetail = row.bloggerDetail;
        const noteInfo = row.noteInfo;
        const userInfo = row.userInfo;
        const noteConent = row.noteContent;

        list.push( {
            '标题':noteInfo['title'],
            '昵称':userInfo['nickName'],
            '发布时间':noteInfo['notePublishTime'],
            '链接':"https://www.xiaohongshu.com/explore/"+noteInfo['noteId'],
            '阅读量':noteInfo['readNum'],
            '曝光量':noteInfo['impNum'],
            '互动量':noteInfo['engageNum'],
            '收藏量':noteInfo['favNum'],
            "点赞量":noteConent['likeNum'],
            "评论量":noteConent['cmtNum'],
            "是否报备":noteInfo['cooperNote']==1?'是':'否',
            "内容类型":noteInfo['noteType']==1?'图文':'视频',
            "小红书ID":bloggerDetail['redId'],
            "内容":noteConent['content']
        } );//redId
    }
    writeExcel('./subject.xlsx',list);
    console.log('完成')
}
